#ifndef DBMANAGER_H
#define DBMANAGER_H

#include <QObject>
#include<QSqlDatabase>
#include<QSqlQuery>
#include<QStringList>
#include <QMutex>
#include"Protocol.h"
class DBManager : public QObject
{
    Q_OBJECT
public:
    explicit DBManager(QObject *parent = nullptr);

    static DBManager*getInstance();
    static void  release();

    void init();        //数据库连接初始化函数
    bool open(const QString dbName, const QString connectName);
    ~DBManager();

public slots:
    void handleRegist(const QString& name,const QString& pwd,int tcpId);
    void handleLogIn(const QString&name,const QString&pwd,int tcpId);
    void handleOffline(const QString &name,int tcpId);
    void handleSearchAllOnlineUsrs(int tcpId);
    void handleSearchAllOnlineFriends(const QString &usrName,int tcpId);
    void handleSearchUsr(const QString &searchName,const QString &usrName,int tcpId);
    void handleAddFriend(const QString &addName,const QString &usrName,int tcpId,PDU*pdu);
    void handleAddFriendAgree(const QString &addName,const QString &usrName);
    void handleFlushFriend(const QString &name,int tcpId);            //处理刷新好友请求
    void handleDelFriend(const QString&usrName,const QString&delName,int currentId,PDU*pdu);
    void handleGroupChatFriendList(const QString &usrName,int tcpId,PDU*pdu);    //获取群聊好友列表
    void handleLogOff(const QString& name,int currentId);

    void handleOnlineForward(const QString &usrName, int tcpId);
    void handleOfflineForward(const QString &usrName, int tcpId);

    void handleModifyUsrInfo(const QString& oldUsrName,
                             const QString& newUsrName,
                             const QString& newPwd,
                             int currentId);
public:

    int getIdByUserName(const QString&name);

signals:
    void registResult(const QString& name,bool result,int tcpId);
    void loginResult(const QString&name,bool result,int tcpId);
    void logoffResult(const QString&name,bool result,int tcpId);
    void usrOfflineResult(const QString &name,bool result,int tcpId);
    void usrModifyInfoResult(const QString &name,bool result,int tcpId);
    void onlineUsrsResult(const QStringList& usrList,int tcpId);
    void onlineFriendsResult(const QStringList& usrList,int tcpId);
    void onlineForwardFriends(const QStringList& usrList,int tcpId);
    void offlineForwardFriends(const QString &name,const QStringList& usrList,int tcpId);
    void searchUsrResult(int flag,int tcpId);
    void addFriendResult(int flag,int tcpId,const QString&addName,PDU*pdu);
    void flushFriendRequest(const QStringList& usrList,int tcpId);
    void delFriendRequest(bool result,int tcpId,const QString&delName,PDU*pdu);
    void groupChatRequest(const QStringList& usrList,int tcpId,PDU*pdu);
    void searchResultReady(QList<UserInfo> resultList,int tcpId);
protected:
    QSqlDatabase m_db;
    QString m_connectName;
    QSqlQuery m_query;
    QString m_lastError;

private:

    // 静态成员声明
    static DBManager* m_instance;
    static QMutex m_mutex;

};

#endif // DBMANAGER_H
